<template>
<!--  导航栏-->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">录入维修车辆</el-button>
  </div>
<!--  主要内容部分-->
    <!--搜索栏  -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="repairSearch.license" @keydown.enter="loadRepairVehicle"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input placeholder="请输入送修人员姓名" style="width:220px;" v-model="repairSearch.username" @keydown.enter="loadRepairVehicle"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width:290px;">
        <el-select v-model="repairSearch.status" placeholder="请选择维修状态" style="width:220px;" @change="loadRepairVehicle">
          <el-option label="维修中" value="0"></el-option>
          <el-option label="已完成" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetRepair">重置</el-button>
        <el-button type="primary" @click="loadRepairVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
    <!--    显示具体信息-->
  <el-card style="margin:20px;">
    <el-table :data="repairVehicle">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="license" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="vehicleRepairStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRepairVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="delRepairVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
    <!--  新增/编辑弹窗-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
              <el-input v-model="saveRepairInfo.username" :disabled="saveRepairInfo.username!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select v-model="saveRepairInfo.license" placeholder="请选择车牌号">
              <el-option v-for="item in licenseList" :value="item.license"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select v-model="saveRepairInfo.item" placeholder="请选择项目">
              <el-option v-for="item in itemList" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select v-model="saveRepairInfo.addr" placeholder="请选择地点">
              <el-option v-for="item in addrList" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择材料" v-model="saveRepairInfo.material">
                <el-option v-for="item in materialList" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input v-model="saveRepairInfo.fee" placeholder="请输入费用"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="6">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveRepairInfo.repairTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="6">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveRepairInfo.status" >
              <el-radio value="0" size="middle">维修中</el-radio>
              <el-radio value="1" size="middle">已完成</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepair">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>

import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//获取当前用户
let user=getUser();
//封装搜索条件的对象
const repairSearch=ref({
  license:'',
  username:'',
  status:''
});
//加载数据的方法
const loadRepairVehicle=()=>{
  let data=qs.stringify(repairSearch.value);
  axios.get(BASE_URL+'/v1/repair/select?'+data)
      .then((res)=>{
        if (res.data.code==2000){
          repairVehicle.value=res.data.data;
        }else {
          ElMessage.error(res.data.msg);
        }
      });
};
//开局加载
onMounted(()=>{
  loadRepairVehicle();
  axios.get(BASE_URL+'/v1/vehicle/select')
        .then((res)=>{
          if (res.data.code==2000){
            licenseList.value=res.data.data;
          }
        });
});
//接收返回数据的数组
const repairVehicle=ref([]);
//车辆维修状态映射
const vehicleRepairStatusFormatter=(row,column,cellValue,index)=>{
  if (cellValue==0){
    cellValue='维修中';
  }else if (cellValue==1){
    cellValue='已完成';
  }
  return cellValue;
};
//重置搜索
const resetRepair=()=>{
  repairSearch.value={};
  loadRepairVehicle();
}

//新增车辆的弹窗
const dialogVisible=ref(false);
//新增车辆弹窗标题
const dialogTitle=ref('录入维修车辆');
//保存新增车辆的信息
const saveRepairInfo=ref({
  license:'',
  item:'',
  addr:'',
  material:'',
  fee:'',
  repairTime:'',
  status:''
});
saveRepairInfo.value.userId=user.id;
saveRepairInfo.value.username=user.username;
//新增维修信息的方法
const saveRepair=()=>{
    let data=qs.stringify(saveRepairInfo.value);
    axios.post(BASE_URL+'/v1/repair/save',data)
          .then((res)=>{
            if (res.data.code==2000){
              ElMessage.success('保存成功');
              dialogVisible.value=false;
              saveRepairInfo.value={};
              saveRepairInfo.value.userId=user.id;
              saveRepairInfo.value.username=user.username;
              loadRepairVehicle();
            }else {
              ElMessage.error(res.data.msg);
            }
          });
};
//控制弹窗消失
const handleClose=()=>{
  if (confirm('确认关闭弹窗?')){
    dialogVisible.value=false;
    saveRepairInfo.value={};
    saveRepairInfo.value.userId=user.id;
    saveRepairInfo.value.username=user.username;
  }
};

//编辑车辆维修信息
const editRepairVehicle=(id)=>{
    dialogTitle.value='编辑维修车辆';
    dialogVisible.value=true;
    saveRepairInfo.value.id=id;
    axios.get(BASE_URL+'/v1/repair/selectById?id='+id)
            .then((res)=>{
              if(res.data.code==2000){
                saveRepairInfo.value=res.data.data;
              }else {
                ElMessage.error(res.data.msg);
              }
            });
}

//删除维修车辆信息
const delRepairVehicle=(id)=>{
  if (confirm('确认删除?')){
    axios.delete(BASE_URL+'/v1/repair/deleteRepair/'+id)
        .then((res)=>{
          if (res.data.code==2000){
            ElMessage.success('删除成功')
            loadRepairVehicle();
          }else {
            ElMessage.error(res.data.msg);
          }
        });
  }
}

//数据选项,后续使用字典项管理
const licenseList=ref([]);
const itemList=ref(['四轮定位','整车清洗']);
const addrList=ref(['广州', '深圳', '佛山']);
const materialList=ref(['轴承', '变速箱', '雨刮器']);


</script>

<style scoped>

</style>